<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>在线考试系统-教师管理</title>
    <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
	<link href="/res/css/style.css" rel="stylesheet">
    <style type="text/css"></style>
</head>
<body>

<?php include(APPLICATION_PATH.'application/views/index/header.html');?>

<div id="container">

    <div class="container-left">
        <?php include(APPLICATION_PATH.'application/views/index/left.html');?>
    </div>

    <div class="container-right well">
		<div class="tabSet"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addteacherModal">添加老师</button></div>
		<table class="table table-bordered">
		   <thead>
			  <tr>
				 <th>姓名</th>
				 <th>教师编号</th>
				 <th>班级</th>
				 <th>课程名称</th>
				 <th>操作</th>
			  </tr>
		   </thead>
		   <tbody>
			   <?php foreach($list as $i) {?>
			   <tr>
				   <td><?php echo $i['name'];?></td>
				   <td><?php echo $i['number'];?></td>
				   <td><?php echo $i['class'];?></td>
				   <td><?php echo $i['course'];?></td>
				   <td>
					   <button type="button" class="btn btn-primary update_teacher" data-toggle="modal" data-target="#updateclassModal" _id="<?php echo $i['_id'];?>" >编辑</button>
					   <button type="button" class="btn btn-danger delete_teacher" data-toggle="modal" data-target="#deleteclassModal" _id="<?php echo $i['_id'];?>">删除</button>
				   </td>
			   </tr>
			   <?php }?>

		   </tbody>
		</table>
    </div>

</div>
<div class="modal fade" id="addteacherModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4 class="modal-title" id="myModalLabel">添加老师</h4>
        </div>
        <div class="modal-body">
           <h5>姓名</h5>
		   <input name="name" class="form-control" type="text" placeholder="" style="width:200px;">
		   <h5>教师编号</h5>
		   <input name="number" class="form-control" type="text" placeholder="" style="width:200px;">
			<h5>班级</h5>
			<select name="class_name">
				<option value="1">--请选择班级--</option>
				<?php foreach($classList as $vo){ ?>
					<option value="<?php echo $vo['name'];?>"><?php echo $vo['name'];?></option>
				<?php }?>
			</select>
		   <h5>课程名称</h5>
			<select name="course">
				<option value="1">--请选择课程--</option>
				<?php foreach($courseList as $vo){ ?>
				<option value="<?php echo $vo['name'];?>"><?php echo $vo['name'];?></option>
				<?php }?>
			</select>
			<h5>密码</h5>
			<input name="password" class="form-control" type="password" placeholder="" style="width:200px;">
			<h5>确认密码</h5>
			<input name="confirm_pwd" class="form-control" type="password" placeholder="" style="width:200px;">
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary" id="addTeacher"> 添加</button>
        </div>
      </div><!-- /.modal-content -->
   </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="modal fade" id="updateclassModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">编辑老师</h4>
			</div>
			<input name="update_data_id" type="hidden">
			<div class="modal-body">
				<h5>姓名</h5>
				<input name="update_name" class="form-control" type="text" placeholder="" style="width:200px;">
				<h5>教师编号</h5>
				<input name="update_number" class="form-control" type="text" placeholder="" style="width:200px;">
				<h5>班级</h5>
				<select name="update_class_name">
					<option value="1">--请选择班级--</option>
					<?php foreach($classList as $vo){ ?>
					<option value="<?php echo $vo['name'];?>"><?php echo $vo['name'];?></option>
					<?php }?>
				</select>
				<h5>课程名称</h5>
				<select name="update_course">
					<option value="1">--请选择课程--</option>
					<?php foreach($courseList as $vo){ ?>
					<option value="<?php echo $vo['name'];?>"><?php echo $vo['name'];?></option>
					<?php }?>
				</select>
				<h5>密码</h5>
				<input name="update_password" class="form-control" type="password" placeholder="" style="width:200px;">
				<h5>确认密码</h5>
				<input name="update_confirm_pwd" class="form-control" type="password" placeholder="" style="width:200px;">
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary" id="saveTeacher">保存</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div>
<div class="modal fade" id="deleteclassModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
				<h4 class="modal-title" id="mySmallModalLabel">删除教师</h4>
			</div>
			<input type="hidden" id="delete_data_id" value=""/>
			<div class="modal-body">
				<button type="button" style="margin-left:35px" class="btn btn-default delete-modal-cancel" data-dismiss="modal">&nbsp;取&nbsp;&nbsp;消&nbsp;&nbsp;</button>
				<button type="button" style="margin-left:30px" class="btn btn-danger delete-modal-ensure">&nbsp;&nbsp;删&nbsp;&nbsp;除&nbsp;&nbsp;</button>
			</div>
		</div><!-- /.modal-content -->
	</div>
</div>
<script src="/res/js/jquery.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>

<script src="/res/js/toast.js"></script>

<script>
$(function () { 
	$('#myModal').modal({
		keyboard: true
	})

	/**
	 * 检查
	 * @param name
	 * @param number
	 * @param class_name
	 * @param course
	 * @param password
	 * @param confirm_pwd
	 * @param action
     * @returns {boolean}
     */
	function check_teacher(name, number, class_name, course, password, confirm_pwd, action){
		if(name == ''){
			new Toast({context:$('body'),top:200 ,time:2000, message:'请输入姓名'}).error();
			return false;
		}

		if(number == ''){
			new Toast({context:$('body'),top:200 ,time:2000, message:'请输入教师编号'}).error();
			return false;
		}

		if(class_name == 1){
			new Toast({context:$('body'),top:200 ,time:2000, message:'请选择班级'}).error();
			return false;
		}

		if(course == 1){
			new Toast({context:$('body'),top:200 ,time:2000, message:'请选择课程'}).error();
			return false;
		}

		if(action == 'add'){
			if(password == ''){
				new Toast({context:$('body'),top:200 ,time:2000, message:'请输入密码'}).error();
				return false;
			}
			if(confirm_pwd == ''){
				new Toast({context:$('body'),top:200 ,time:2000, message:'请确认密码'}).error();
				return false;
			}
		}

		if(password != confirm_pwd ){
			new Toast({context:$('body'),top:200 ,time:2000, message:'两次密码输入不一致,请重新输入'}).error();
			$('[name="password"]').val('');
			$('[name="confirm_pwd"]').val('');
			return false;
		}

		return true;
	}

	$('#addTeacher').click(function(){
		var number = $('[name="number"]').val();
		var name = $('[name="name"]').val();
		var class_name = $('[name="class_name"]').val();
		var course = $('[name="course"]').val();
		var password = $('[name="password"]').val();
		var confirm_pwd = $('[name="confirm_pwd"]').val();

		if(!check_teacher(name, number, class_name, course, password, confirm_pwd, 'add')){
			return false;
		}

		var url = '/teacher/add';
		var data = {"number":number, "name":name, "class":class_name, "course":course, "password":password};
		$.post(url, data, function(ret){
			if(ret.en == 200) {
				new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).success();
				setTimeout(function(){
					window.location.href = '/teacher/index';
				} , 2000);
			} else {
				new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
			}
		});
	});

	/** "修改" 触发模态框显示 */
	$('.update_teacher').click(function(){
		var _id = $(this).attr('_id');
		$('[name="update_data_id"]').val(_id);
		$('[name="update_course"]').val($(this).parent().prev().text());
		$('[name="update_class_name"]').val($(this).parent().prev().prev().text());
		$('[name="update_number"]').val($(this).parent().prev().prev().prev().text());
		$('[name="update_name"]').val($(this).parent().prev().prev().prev().prev().text());
	});

	/**
	 * 编辑老师，并保存
	 */
	$('#saveTeacher').click(function(){
		var update_data_id = $('[name="update_data_id"]').val();
		var update_number = $('[name="update_number"]').val();
		var update_name = $('[name="update_name"]').val();
		var update_class_name = $('[name="update_class_name"]').val();
		var update_course = $('[name="update_course"]').val();
		var update_password = $('[name="update_password"]').val();
		var update_confirm_pwd = $('[name="update_confirm_pwd"]').val();

		if(!check_teacher(update_name, update_number, update_class_name, update_course, update_password, update_confirm_pwd,'update')){
			return false;
		}

		var url = '/teacher/save';
		var data = {"_id":update_data_id, "number":update_number, "name":update_name, "class":update_class_name, "course":update_course, "password":update_password};
		$.post(url, data, function(ret){
			if(ret.en == 200) {
				new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).success();
				setTimeout(function(){
					window.location.href = '/teacher/index';
				} , 2000);
			} else {
				new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
			}
		});
	});

	/** "删除" 触发模态框显示 */
	$('.delete_teacher').click(function(){
		var _id = $(this).attr('_id');
		$('#delete_data_id').val(_id);
	});

	/** 执行 "删除" */
	$('.delete-modal-ensure').click(function(){
		var _id = $('#delete_data_id').val();
		var request_api = '/teacher/delete';
		if(_id != undefined && _id != '') {
			$.post(request_api ,{"_id":_id} ,function(ret) {
				if(ret.en == 200) {
					new Toast({context:$('body'),top:200 ,time:1000 ,message:ret.em}).success();
					setTimeout(function(){
						$('#deleteclassModal').modal('hide');
						window.location.reload();
					} , 1000);
				} else {
					new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
				}
			});
		} else {
			new Toast({context:$('body'),top:200 ,time:1000, message:"参数不全"}).error();
		}
	});
});
</script>
</body>
</html>